#from re import T
import pygame,sys,random,time
pygame.init()

def shua():
    pygame.display.flip()
w=[600,800]
x=[0,30]
y,c=[0,20],[255,0,0]
x2=50
ball=[random.randint(100,500),random.randint(100,500),4,4,False,3,0,0,0,0]
screen=pygame.display.set_mode(w)
screen.fill((255,255,255))
shua()

def ballzhuang(ball,x):
    if ball[0]<=10:
        ball[2]=-(ball[2])
    if ball[0]>=590:
        ball[2]=-(ball[2])
    if ball[1]<=10:
        ball[3]=-(ball[3])
    if ball[1]>770:
        if ball[0]<x-70 or ball[0]>x+70:
            ball[4]=True
            ball[5]-=1
            ball[8]+=1
        elif ball[7]==0:
            ball[3]=-(ball[3])
            ball[6]+=-(ball[3])/2
            ball[9]+=1
        ball[7]=7
    if ball[7]>0:
        ball[7]-=1
    ball[0]+=ball[2]
    ball[1]+=ball[3]
    return ball

def drawflip(screen,c,x,y):
    pygame.draw.rect(screen,c,[int(x[0]),int(y[0]),int(x[1]-x[0]),int(y[1]-y[0])],0)
    #shua()

def draw(screen,x,y,x1,y1,c):#x,y,列表。x1,y1,鼠标位置，c,颜色
    drawflip(screen,c,x,y)
    if x1>x[0] and x1<x[1]:
        if y1>y[0] and y1<y[1]:
            return True
        else:
            return False
    else:
        return False

def pygameprint(screen,p,pygame,daxiao,c,weizhi):
    score=str(p)
    font = pygame.font.Font(None, daxiao)
    my_score = font.render(str(score), False, c)    #设置分数显示，白色
    screen.blit(my_score, (weizhi[0], weizhi[1])) 
    shua()

def drawlines(pygame,screen):
    pygame.draw.lines(screen,[255,255,255],True,[[int(x[0]),int(y[0])],[int(x[1]),int(y[1])]],2)
    pygame.draw.lines(screen,[255,255,255],True,[[int(x[1]),int(y[0])],[int(x[0]),int(y[1])]],2)

drawflip(screen,c,x,y)
drawlines(pygame,screen)
shua()

def w1(screen,pygame,w):
    z1=[200,400,500,550]
    c=[255,0,0]
    drawflip(screen,c,[z1[0],z1[1]],[z1[2],z1[3]])
    pygameprint(screen,"play",pygame,60,[255,255,255],[z1[0]+50,z1[2]])
    shua()
    r=True
    while r:
        for event in pygame.event.get():
            if  event.type == pygame.MOUSEBUTTONUP:
                if event.button==1:
                    x1,y1=event.pos
                    if draw(screen,x,y,x1,y1,c):
                        r=False
                        pygame.quit()
                    elif draw(screen,[z1[0],z1[1]],[z1[2],z1[3]],x1,y1,c):
                        r=False
                    else:
                        drawflip(screen,c,[z1[0],z1[1]],[z1[2],z1[3]])
                        drawflip(screen,c,x,y)
                        drawlines(pygame,screen)
                        pygameprint(screen,"play",pygame,60,[255,255,255],[z1[0]+50,z1[2]])
        shua()
    pygame.draw.rect(screen,[255,255,255],[0,0,int(w[0]),int(w[1])],0) 
    drawflip(screen,c,x,y)
    drawlines(pygame,screen)
    shua()

w1(screen,pygame,w)
clock=pygame.time.Clock()
r=True
while r:
    #clock.tick(100)
    #time.sleep(0.001)
    for event in pygame.event.get():
        if  event.type == pygame.MOUSEBUTTONUP:
            if event.button==1:
                x1,y1=event.pos
                if draw(screen,x,y,x1,y1,c):
                    r=False
    #pygame.draw.rect(screen,[255,255,255],[0,700,800,100],0)
    #pygame.draw.rect(screen,[255,255,255],[ball[0]-10,ball[1]-10,20,20],0)
    #pygame.draw.rect(screen,[255,255,255],[300,0,200,100])
    screen.fill((255,255,255))
    x2,y2 = pygame.mouse.get_pos()
    drawflip(screen,c,[0,30],[0,20])
    drawlines(pygame,screen)
    print(x2)
    print(ball)
    ball=ballzhuang(ball,x2)
    pygame.draw.rect(screen,[0,255,0],[x2-70,780,140,20],0)
    pygame.draw.circle(screen, [255,0,0], [ball[0],ball[1]], 10)

    font = pygame.font.Font(None, 60)
    my_score = font.render("score:", True, c)    #设置分数显示，白色
    screen.blit(my_score, (350, 0))
    score=int(ball[6])
    my_score = font.render(str(score), True, c)    #设置分数显示，白色
    screen.blit(my_score, (500, 0))
    weizhi=ball[5]
    w=350
    while weizhi>0:
        pygame.draw.circle(screen,[255,0,0],[w+10,50],10)
        w+=30
        weizhi-=1

    while ball[4]:
        if ball[5]>0:
            drawflip(screen,c,[200,400],[500,550])
            drawflip(screen,c,x,y)
            pygameprint(screen,"play",pygame,60,[255,255,255],[250,500])
            pygame.display.update()
            for event in pygame.event.get():
                if  event.type == pygame.MOUSEBUTTONUP:
                    if event.button==1:
                        x1,y1=event.pos
                        if draw(screen,[200,400],[500,550],x1,y1,c):
                            ball[4]=False
                            ball[1]=random.randint(100,500)
                            ball[0]=random.randint(100,500)
                            pygame.draw.circle(screen, [255,0,0], [ball[0],ball[1]], 10)
                            pygame.display.update()
                            time.sleep(0.5)
                        if draw(screen,x,y,x1,y1,c):
                            pygame.quit()
        if ball[5]<=0:
            screen.fill((255,255,255))
            my_score = font.render("game.over", True, c)    #设置分数显示，白色
            screen.blit(my_score, (200, 450))
            pygame.display.update()
            time.sleep(2)
            w1(screen,pygame,[600,800])
            ball=[random.randint(100,500),random.randint(100,500),4,4,False,3,0,0,0,0]

        drawflip(screen,c,[200,400],[500,550])
        drawflip(screen,c,x,y)
    if ball[9]>=5:
        ball[5]+=1
        ball[9]-=5
        if ball[3]<10 and ball[3]>-10:
            if ball[3]>0:
                ball[3]+=2
            if ball[3]<0:
                ball[3]-=2
            if ball[2]>0:
                ball[2]+=2
            if ball[2]<0:
                ball[2]-=2
    pygame.display.update()           #更新窗口，保证窗口始终打开
    #time.sleep(0.005)  
    clock.tick(50)
pygame.quit()




"""for event in pygame.event.get():
            if event.type==pygame.QUIT:
                run=False
            elif 
                # print(event.button)
                :print(event.pos)
                    x,y=event.pos
event.type==pygame.QUIT:
            t=False"""